package b1.scenes.main
{
	import b1.net.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.Timer;

	public class ThoughtBubble extends Sprite
	{
		private var tickCount:int;
		private var bubbles:Array;
		private const randRange:int = 40;
		private const tickCountMax:int = 10;
		private var timer:Timer;
		
		public function ThoughtBubble(text:String)
		{
			super();
			bubbles = new Array();
			tickCount = Math.random() * tickCountMax;
			//第一个气泡
			var Bubble:Class = ResourceHelper.getInstance().getClassByName("Bubble");
			var bubble:Sprite = new Bubble();
			bubble.width = 120;
			bubble.height = 100;
			bubble.x = -30 + Math.random() * randRange;
			bubble.y = 0;
			var thoughtText:TextField = new TextField();
			thoughtText.selectable = false;
        	thoughtText.autoSize = TextFieldAutoSize.LEFT;
			thoughtText.defaultTextFormat = new TextFormat("微软雅黑", 20, 0x000000, true);
			thoughtText.text = text;
			thoughtText.x = (bubble.width - thoughtText.width) / 2;
			thoughtText.y = (bubble.height - thoughtText.height) / 2;
			bubble.addChild(thoughtText);
			bubbles.push(bubble);
			addChild(bubble);
			//第二个气泡
			bubble = new Bubble();
			bubble.width = 60;
			bubble.height = 50;
			bubble.x = Math.random() * randRange;
			bubble.y = 120;
			bubbles.push(bubble);
			addChild(bubble);
			//第三个气泡
			bubble = new Bubble();
			bubble.width = 20;
			bubble.height = 18;
			bubble.x = 20 + Math.random() * randRange;
			bubble.y = 180;
			bubbles.push(bubble);
			addChild(bubble);
			//时钟
			timer = new Timer(400);
			timer.addEventListener(TimerEvent.TIMER, onTick);
			timer.start();
		}
		
		public function mouseOver():void
		{
			timer.stop();
			bubbles[0].visible = true;
			bubbles[1].visible = true;
			bubbles[2].visible = true;
		}
		
		public function mouseOut():void
		{
			timer.start();
		}
		
		private function onTick(event:Event):void
		{
			tickCount = (tickCount + 1) % tickCountMax;
			if (tickCount == 1)
			{
				bubbles[0].visible = false;
				bubbles[1].visible = false;
				bubbles[2].visible = false;
			}
			else if (tickCount == 2)
			{
				bubbles[0].visible = false;
				bubbles[1].visible = false;
				bubbles[2].visible = true;
			}
			else if (tickCount == 3)
			{
				bubbles[0].visible = false;
				bubbles[1].visible = true;
				bubbles[2].visible = true;
			}
			else
			{
				bubbles[0].visible = true;
				bubbles[1].visible = true;
				bubbles[2].visible = true;
			}
		}
	}
}